﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using System.Text.RegularExpressions;
using web = System.Web;

using XStudio.Core.Ext.ConvertExtensions;
using Core.API.Entity;

namespace Api.Base
{
    public class BaseApiController : ApiController
    {
        /// <summary>
        /// 用户验证
        /// </summary> 
        /// <returns>表单参数的值</returns>
        public RespBaseModel UserVerify()
        {
            RespBaseModel resp = new RespBaseModel();
            resp.IsOK = true;
            resp.ErrorCode = 0;
            resp.Message = "正常";
            if (this.UserID < 0)
            {
                resp.IsOK = false;
                resp.ErrorCode = 400;
                resp.Message = "用户未登录或授权过期";
            }
            return resp;
        }


        /// <summary>
        /// 获取当前登录用户标识
        /// </summary>
        /// <value>The user ID.</value>
        protected virtual int UserID
        {
            get
            {
                return CurrentUser.UserID;
            }
        }

        /// <summary>
        /// 获取当前用户账户
        /// </summary>
        /// <value>The user name.</value>
        protected virtual string Account
        {
            get
            {
                return CurrentUser.Account;
            }
        }

        /// <summary>
        /// 获取当前用户账户
        /// </summary>
        /// <value>The user name.</value>
        protected virtual string UserName
        {
            get
            {
                return CurrentUser.UserName;
            }
        }

        /// <summary>
        /// 获取当前授权码
        /// </summary>
        protected virtual string Token
        {
            get
            {
                return CurrentUser.Token;
            }
        }

        /// <summary>
        /// 获取指定用户信息
        /// </summary>
        public virtual UserEntity CurrentUser
        {
            get
            {
                //默认
                UserEntity curUser = null;

                //如果身份认证已通过，并且会话为空，执行已授权用户登录
                if (web.HttpContext.Current.Session["CurrentUser"] == null || web.HttpContext.Current.Session["CurrentUser"].GetType() != typeof(UserEntity))
                {

                }
                else
                {
                    //获取授权的用户信息
                    curUser = web.HttpContext.Current.Session["CurrentUser"] as UserEntity;
                }

                if (curUser == null)
                {
                    curUser = new UserEntity()
                    {
                        UserID = -1,
                        Account = "",
                        UserName = "",
                        Token = ""
                    };
                }
                //返回
                return curUser;
            }
        }

        /// <summary>
        ///   获得指定表单参数的值
        /// </summary>
        /// <param name = "strName">表单参数</param>
        /// <returns>表单参数的值</returns>
        public virtual string GetRequestString(string strName)
        {
            return HttpContext.Current.Request[strName] ?? "";
        }

        /// <summary>
        ///   获得指定表单参数的值
        /// </summary>
        /// <param name = "strName">表单参数</param>
        /// <param name = "defValue">缺省值</param>
        /// <returns>表单参数的值</returns>
        public virtual string GetRequestString(string strName, string defValue)
        {
            return HttpContext.Current.Request[strName] ?? defValue;
        }

        /// <summary>
        ///   获得指定Url参数的int类型值
        /// </summary>
        /// <param name = "strName">Url参数</param>
        /// <param name = "defValue">缺省值</param>
        /// <returns>Url参数的int类型值</returns>
        public virtual int GetRequestInt(string strName, int defValue)
        {
            int result = 0;
            int.TryParse(HttpContext.Current.Request[strName], out result);
            if (result > 0)
            {
                return result;
            }
            return defValue;
        }

        /// <summary>
        /// 获取用户的IP
        /// </summary>
        /// <value>The user IP.</value>
        public virtual string UserIP
        {
            get
            {
                return System.Web.HttpContext.Current.Request.UserHostAddress;
            }
        }

        ///// <summary>
        /////  GET 输出内容
        ///// </summary>
        ///// <param name = "data">输出对象</param> 
        ///// <returns>输出JSON</returns>
        //public virtual object Resp2Get(object data)
        //{
        //    return Json(data);
        //}

        ///// <summary>
        /////  POST 输出内容
        ///// </summary>
        ///// <param name = "data">输出对象</param> 
        ///// <returns>输出JSON</returns>
        //public virtual object Resp2POST(object data)
        //{
        //    return Json(data);
        //}

    }
}
